var states=-1;
var oContent=new Array();

var myPlaylist=null;

var myPlaylist_num=new Array();

$(document).ready(function () {
    changeColorLayout(0);
    myPlaylist = new jPlayerPlaylist({
        jPlayer: "#jplayer_N",
        cssSelectorAncestor: "#jp_container_N"
    }, null, {
        playlistOptions: {
            enableRemoveControls: true,
            autoPlay: true
        },
        swfPath: "js/jPlayer",
        supplied: "webmv, ogv, m4v, oga, mp3",
        smoothPlayBar: true,
        keyEnabled: false,
        audioFullScreen: false
    });

    $(document).on($.jPlayer.event.pause, myPlaylist.cssSelector.jPlayer,  function(){
        $('.musicbar').removeClass('animate');
        $('.jp-play-me').removeClass('active');
        $('.jp-play-me').parent('li').removeClass('active');
    });

    $(document).on($.jPlayer.event.play, myPlaylist.cssSelector.jPlayer,  function(){
        $('.musicbar').addClass('animate');
    });

    $(document).on('click', '.jp-play-me', function(e){
        e && e.preventDefault();
        var $this = $(e.target);
        if (!$this.is('a')) $this = $this.closest('a');

        $('.jp-play-me').not($this).removeClass('active');
        $('.jp-play-me').parent('li').not($this.parent('li')).removeClass('active');

        $this.toggleClass('active');
        $this.parent('li').toggleClass('active');
        if( !$this.hasClass('active') ){
            myPlaylist.pause();
        }else{
            var i = Math.floor(Math.random() * (1 + 6 - 1));
            myPlaylist.play(i);
        }

    });



    // video

    $("#jplayer_1").jPlayer({
        ready: function () {
            $(this).jPlayer("setMedia", {
                title: "Big Buck Bunny",
                m4v: "http://flatfull.com/themes/assets/video/big_buck_bunny_trailer.m4v",
                ogv: "http://flatfull.com/themes/assets/video/big_buck_bunny_trailer.ogv",
                webmv: "http://flatfull.com/themes/assets/video/big_buck_bunny_trailer.webm",
                poster: "images/m41.jpg"
            });
        },
        swfPath: "js",
        supplied: "webmv, ogv, m4v",
        size: {
            width: "100%",
            height: "auto",
            cssClass: "jp-video-360p"
        },
        globalVolume: true,
        smoothPlayBar: true,
        keyEnabled: true
    });
});



function refresh()
{
    switch (states)
    {
        case 0:query_index(true);break;
        case 1:query_Genres(true);break;
        case 2:query_Singers(true);break;
        case 3:query_Lists(true);break;
        case 4:query_Videos(true);break;
        case 5:query_PersonCenter(true);break;
        default:
            break;
    }
}





function resetColor()
{
    $('#nav').removeClass('bg-dark');
    $('#nav').removeClass('bg-black');
    $('#navbar').removeClass('bg-black lter');
    $('#navbar').removeClass('bg-white-only');
    $('#navbar-header').removeClass('bg-info');
    $('#navbar-header').removeClass('bg-success');
    $('#player').removeClass('bg-info dker');
    $('#player').removeClass('bg-dark');
    $('#player').removeClass('bg-success');
}

function addList_play(id)
{
    $.ajax({
        url:"{:url('index/getMusicByMid')}",
        type:"get",
        data:{
            mid:id
        },
        success:function(data)
        {
            addPlayList(data.mname,data.sname,data.file);
        },
        error:function()
        {

        },
        dataType: "json"
    });
}

function query_Genres_con(tid,pos)
{
    $.ajax({
        url:"{:url('genres/getCon_Genres')}",
        type:'get',
        data:{
            tid:tid
        },
        success:function(data)
        {
            $('#genres_content').html(data);
        },
        dataType:'html'
    });
    $('#genres_slide a').removeClass('active');
    $('#genres_slide a').eq(pos).addClass('active');
}


function query_index(flag=false)
{
    if(states==0 && !flag)
        return;
    if((oContent[0]==null) || flag)
    {
        $.ajax({
            url:"{:url('index/getContent')}",
            type:"post",
            data:null,
            success:function(data)
            {
                oContent[0]=data;
                $('#container').html(data);
            },
            error:function()
            {
                if(oContent[0]==null)
                    $('#container').html('网络访问受限');
                else
                    $('#container').html(oContent[0]);
            },
            dataType: "html"
        });
    }
    else
    {
        $('#container').html(oContent[0]);
    }
    states=0;
}

function query_Genres(flag=false)
{
    if(states==1 && !flag)
        return;
    if((oContent[1]==null) || flag)
    {
        $.ajax({
            url:"{:url('genres/getContent')}",
            type:"post",
            data:null,
            success:function(data)
            {
                oContent[1]=data;
                $('#container').html(data);
            },
            error:function()
            {
                if(oContent[1]==null)
                    $('#container').html('网络访问受限');
                else
                    $('#container').html(oContent[1]);
            },
            dataType: "html"
        });
    }
    else
    {
        $('#container').html(oContent[1]);
    }
    query_Genres_con(0,0);
    states=1;
}

function query_Singers(flag=false)
{
    if(states==2 && !flag)
        return;
    if((oContent[2]==null) || flag)
    {
        $.ajax({
            url:"{:url('genres/getContent')}",
            type:"post",
            data:null,
            success:function(data)
            {
                oContent[2]=data;
                $('#container').html(data);
            },
            error:function()
            {
                if(oContent[2]==null)
                    $('#container').html('网络访问受限');
                else
                    $('#container').html(oContent[2]);
            },
            dataType: "html"
        });
    }
    else
    {
        $('#container').html(oContent[2]);
    }
    states=2;
}

function query_Lists(flag=false)
{
    if(states==3 && !flag)
        return;
    if((oContent[3]==null) || flag)
    {
        $.ajax({
            url:"{:url('lists/getContent')}",
            type:"post",
            data:null,
            success:function(data)
            {
                oContent[3]=data;
                $('#container').html(data);
            },
            error:function()
            {
                if(oContent[3]==null)
                    $('#container').html('网络访问受限');
                else
                    $('#container').html(oContent[3]);
            },
            dataType: "html"
        });
    }
    else
    {
        $('#container').html(oContent[3]);
    }
    states=3;
}

function query_Videos(flag=false)
{
    if(states==4 && !flag)
        return;
    if((oContent[4]==null) || flag)
    {
        $.ajax({
            url:"{:url('video/getContent')}",
            type:"post",
            data:null,
            success:function(data)
            {
                oContent[4]=data;
                $('#container').html(data);
            },
            error:function()
            {
                if(oContent[4]==null)
                    $('#container').html('网络访问受限');
                else
                    $('#container').html(oContent[4]);
            },
            dataType: "html"
        });
    }
    else
    {
        $('#container').html(oContent[4]);
    }
    states=4;
}

function query_PersonCenter(flag=false)
{
    if(states==5 && !flag)
        return;
    if((oContent[5]==null) || flag)
    {
        $.ajax({
            url:"{:url('video/getContent')}",
            type:"post",
            data:null,
            success:function(data)
            {
                oContent[5]=data;
                $('#container').html(data);
            },
            error:function()
            {
                if(oContent[5]==null)
                    $('#container').html('网络访问受限');
                else
                    $('#container').html(oContent[5]);
            },
            dataType: "html"
        });
    }
    else
    {
        $('#container').html(oContent[5]);
    }
    states=5;
}


function changeColorLayout(i)
{
    resetColor();
    $('#player').show();
// case 'Genres': echo'';  case 'Index': echo 'bg-dark';break;case 'Lists': case 'Video' : echo 'bg-black'; break; default :  echo 'bg-black';break;
// case 'Genres': echo'';  case 'Index': echo 'bg-white-only';break;case 'Lists': case 'Video' : echo 'bg-black lter'; break; default :  echo 'bg-white-only';break;
// case 'Genres': echo'';  case 'Index': echo 'bg-info';break;case 'Lists': case 'Video' : echo 'bg-success'; break; default :  echo 'bg-info';break;
// case 'Genres': echo'bg-info dker'; break;  case 'Index': echo 'bg-dark';break;case 'Lists': echo 'bg-success dker';break; default :  echo 'bg-dark';break;
    switch (i)
    {
        case 0:
            $('#nav').addClass('bg-dark');
            $('#navbar').addClass('bg-white-only');
            $('#navbar-header').addClass('bg-info');
            $('#player').addClass('bg-dark');
            $('title').html("Musik | Index");
            query_index();
            break;
        case 1:
            $('#nav').addClass('bg-dark');
            $('#navbar').addClass('bg-white-only');
            $('#navbar-header').addClass('bg-info');
            $('#player').addClass('bg-info dker');
            $('title').html("Musik | Gendres");
            query_Genres();
            break;
        case 2:
            $('#nav').addClass('bg-black');
            $('#navbar').addClass('bg-black lter');
            $('#navbar-header').addClass('bg-info');
            $('#player').addClass('bg-success');
            $('title').html("Musik | PlayList");
            query_Singers();
            break;
        case 3:
            $('#nav').addClass('bg-black');
            $('#navbar').addClass('bg-black lter');
            $('#navbar-header').addClass('bg-success');
            $('#player').addClass('bg-success dker');
            $('title').html("Musik | PlayList");
            query_Lists();
            break;
        case 4:
            $('#nav').addClass('bg-black');
            $('#navbar').addClass('bg-black lter');
            $('#navbar-header').addClass('bg-success');
            $('title').html("Musik | Videos");
            $('#player').hide();
            query_Videos();
            break;
        case 5:
            $('#nav').addClass('bg-dark');
            $('#navbar').addClass('bg-white-only');
            $('#navbar-header').addClass('bg-success');
            $('title').html("Musik | Videos");
            $('#player').hide();
            query_PersonCenter();
            break;
        default:
            $('#nav').addClass('bg-dark');
            $('#navbar').addClass('bg-white-only');
            $('#navbar-header').addClass('bg-info');
            $('#player').addClass('bg-dark');
            query_index();
            break;
    }
}

function addPlayList(titie,artist,mp3_name)
{
    console.log(titie);
    console.log(artist);
    console.log(mp3_name);
    myPlaylist.pause();
    myPlaylist.add({
        title:titie,
        artist:artist,
        mp3:"http://localhost/Musik/public/static/MP3/"+mp3_name,
    },true);
}